package com.example.hudongwx.myclass0719application.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresPermission;
import android.support.v4.app.Fragment;
import android.support.v4.media.RatingCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.hudongwx.myclass0719application.API;
import com.example.hudongwx.myclass0719application.Brow1Activity;
import com.example.hudongwx.myclass0719application.Brow2Activity;
import com.example.hudongwx.myclass0719application.Json.MyJsonObj;
import com.example.hudongwx.myclass0719application.Json.MyResponseParser;
import com.example.hudongwx.myclass0719application.R;
import com.example.hudongwx.myclass0719application.SexActivity;
import com.example.hudongwx.myclass0719application.UserActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.x;

/**
 * Created by hudongwx on 16-10-26.
 */
@ContentView(R.layout.fragshanjiang)
public class FragShanJiang extends Fragment {
    private String TAG = "FragShanJiang";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = x.view().inject(this, inflater, container);
        Log.i(TAG, "onCreateView: ");

        final ListView lv = (ListView) view.findViewById(R.id.fragshanjiang_listview);

        RequestParams requestParams = new RequestParams(API.sjImgUrl);


        x.http().get(requestParams, new Callback.CommonCallback<MyJsonObj>() {
            @Override
            public void onSuccess(MyJsonObj result) {

                Log.i(TAG, "onSuccess: ");

                lv.setAdapter(new Myadpater(result));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i(TAG, "onError: ");
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.i(TAG, "onCancelled: ");
            }

            @Override
            public void onFinished() {
                Log.i(TAG, "onFinished: ");
            }
        });
        return view;

    }

    /**
     * 跳转到注册登陆界面
     * @param v
     */
    @Event(R.id.fragshanjiang_imageview_like)
    private void goUserActivity(View v){

        startActivity(new Intent(getActivity(), UserActivity.class));

    }

    /**
     * 设置性别的弹出窗口
     * @param view
     */
     @Event(R.id.fragshanjiang_imageview_sex)
     private  void setSex(View view){
     Intent intent =new Intent(getActivity(), SexActivity.class);
         startActivity(intent);

     }

    class Myadpater extends BaseAdapter {

         JSONObject jsonObject;
      JSONArray data;
        public Myadpater(JSONObject jsonObject) {
            this.jsonObject = jsonObject;
        }


        //第一个视图的方法
        public View getFirstItem() {
            OnClickfirst onClickfirst=new OnClickfirst();
            ImageView imageView1, imageView2, imageView3, imageView4, imageView5;
            View view = getActivity().getLayoutInflater().inflate(R.layout.fragshanjiang_list_item_1, null);
            //获取控件
            LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.fragshanjiang_ll);
            imageView1 = (ImageView) view.findViewById(R.id.fragshanjiang_imageview_1);
            imageView2 = (ImageView) view.findViewById(R.id.fragshanjiang_imageview_2);
            imageView3 = (ImageView) view.findViewById(R.id.fragshanjiang_imageview_3);
            imageView4 = (ImageView) view.findViewById(R.id.fragshanjiang_imageview_4);
            imageView5 = (ImageView) view.findViewById(R.id.fragshanjiang_imageview_5);


            try {
                JSONArray jsonArray = jsonObject.getJSONArray("templates").getJSONObject(0).getJSONArray("item");
                int len = jsonArray.length();
                LinearLayout.LayoutParams params = new RadioGroup.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.weight = 1;

                //5个小图标
                for (int i = 0; i < len; i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String imgUrl = jsonObject.getString("imgUrl");

                    Log.i(TAG, "getFirstItem: " + imgUrl);

                    ImageView iv = new ImageView(getActivity());
                    iv.setScaleType(ImageView.ScaleType.FIT_XY);
                    iv.setAdjustViewBounds(true);
                    iv.setLayoutParams(params);

                    x.image().bind(iv, imgUrl);
                    //保存数据
                    iv.setTag(jsonObject.getString("link"));

                    iv.setOnClickListener(onClickfirst);
                    linearLayout.addView(iv);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            OnClickSecond onClickSecond=new OnClickSecond();
            try {
                //4个 大图片的 json
                JSONArray jsonArray1 = jsonObject.getJSONArray("templates").getJSONObject(1).getJSONArray("item");
                //9:30的json
                JSONObject jsonObject2 = this.jsonObject.getJSONArray("templates").getJSONObject(2).getJSONArray("item").getJSONObject(0);

                String imgurl5 = jsonObject2.getString("imgUrl");
                String imgUrl1 = jsonArray1.getJSONObject(0).getString("imgUrl");
                String imgUrl2 = jsonArray1.getJSONObject(1).getString("imgUrl");
                String imgUrl3 = jsonArray1.getJSONObject(2).getString("imgUrl");
                String imgUrl4 = jsonArray1.getJSONObject(3).getString("imgUrl");
                //传输值
                imageView1.setTag(jsonArray1.getJSONObject(0).getString("link"));
                imageView2.setTag(jsonArray1.getJSONObject(1).getString("link"));
                imageView3.setTag(jsonArray1.getJSONObject(2).getString("link"));
                imageView4.setTag(jsonArray1.getJSONObject(3).getString("link"));

                //设置点击事件
                imageView1.setOnClickListener(onClickSecond);
                imageView2.setOnClickListener(onClickSecond);
                imageView3.setOnClickListener(onClickSecond);
                imageView4.setOnClickListener(onClickSecond);
                //显示图片
                x.image().bind(imageView1, imgUrl1);
                x.image().bind(imageView2, imgUrl2);
                x.image().bind(imageView3, imgUrl3);
                x.image().bind(imageView4, imgUrl4);
                x.image().bind(imageView5, imgurl5);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return view;
        }



        @Override
        public int getCount() {
            int  data = 0;
            try {
              data = jsonObject.getJSONArray("data").length();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return  data;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {


            if (0 == position) {
                convertView = getFirstItem();
                return convertView;
            }else{


                try {
                    data = jsonObject.getJSONArray("data");
                        ImageView image=new ImageView(getActivity());
                        String url=data.getJSONObject(position-1).getString("imgUrl");



                        x.image().bind(image,url);
                         convertView=image;
                        return convertView;


                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }


            return new TextView(getActivity());
        }
    }
    //第一个点击事件类
    class OnClickfirst implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(getActivity(), Brow1Activity.class);
            intent.putExtra("url",v.getTag()+"");
            startActivity(intent);

        }
    }
    //四个图片的点击事件
    class OnClickSecond implements View.OnClickListener{

        @Override
        public void onClick(View v) {
           Intent intent =new Intent(getActivity(), Brow2Activity.class);
            intent.putExtra("url",v.getTag()+"");
            startActivity(intent);
        }
    }



}
